/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author jacopo
 */
@Entity
@Table(name = "Notification")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Notification.findAll", query = "SELECT n FROM Notification n"),
    @NamedQuery(name = "Notification.findById", query = "SELECT n FROM Notification n WHERE n.id = :id"),
    @NamedQuery(name = "Notification.findByType", query = "SELECT n FROM Notification n WHERE n.type = :type"),
    @NamedQuery(name = "Notification.findByRead", query = "SELECT n FROM Notification n WHERE n.read = :read"),
    @NamedQuery(name = "Notification.findBySendingDate", query = "SELECT n FROM Notification n WHERE n.sendingDate = :sendingDate"),
    @NamedQuery(name = "Notification.findByAccepted", query = "SELECT n FROM Notification n WHERE n.accepted = :accepted"),
    @NamedQuery(name = "Notification.findByNextSunnyDay", query = "SELECT n FROM Notification n WHERE n.nextSunnyDay = :nextSunnyDay")})
public class Notification implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "Id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "Type")
    private String type;
    @Basic(optional = false)
    @NotNull
    @Column(name = "`Read`")
    private boolean read;
    @Basic(optional = false)
    @NotNull
    @Column(name = "SendingDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date sendingDate;
    @Column(name = "Accepted")
    private Boolean accepted;
    @Column(name = "NextSunnyDay")
    @Temporal(TemporalType.TIMESTAMP)
    private Date nextSunnyDay;
    @JoinTable(name = "Notification_User", joinColumns = {
        @JoinColumn(name = "Notification_Id", referencedColumnName = "Id")}, inverseJoinColumns = {
        @JoinColumn(name = "User_Email", referencedColumnName = "Email")})
    @ManyToMany
    private Collection<User> userCollection;
    @JoinColumn(name = "Event_Id", referencedColumnName = "Id")
    @ManyToOne(optional = false)
    private Event eventId;

    /**
     * This class is a simple collection of static types that can be assigned 
     * to the entity. As {@link Notification#type} should be assigned only this values.
     * 
     * 
     */
    public static class TYPE {

        public static final String ONE_DAY_NOTIFICATION = "oneDay";
        public static final String THREE_DAY_NOTIFICATION = "threeDay";
        public static final String INVITATION_NOTIFICATION = "invitation";
        public static final String WEATHER_CHANGED = "weatherChanged";
        //Accessory array for easyest cheking
        public static String[] CONST_ARRAY = {ONE_DAY_NOTIFICATION,
            THREE_DAY_NOTIFICATION,
            INVITATION_NOTIFICATION,
            WEATHER_CHANGED};
    }

    public Notification() {
    }

    public Notification(String type, boolean read, Date sendingDate) {
        this.type = type;
        this.read = read;
        this.sendingDate = sendingDate;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean getRead() {
        return read;
    }

    public void setRead(boolean read) {
        this.read = read;
    }

    public Date getSendingDate() {
        return sendingDate;
    }

    public void setSendingDate(Date sendingDate) {
        this.sendingDate = sendingDate;
    }

    public Boolean getAccepted() {
        return accepted;
    }

    public void setAccepted(Boolean accepted) {
        this.accepted = accepted;
    }

    public Date getNextSunnyDay() {
        return nextSunnyDay;
    }

    public void setNextSunnyDay(Date nextSunnyDay) {
        this.nextSunnyDay = nextSunnyDay;
    }

    @XmlTransient
    public Collection<User> getUserCollection() {
        return userCollection;
    }

    public void setUserCollection(Collection<User> userCollection) {
        this.userCollection = userCollection;
    }

    public Event getEventId() {
        return eventId;
    }

    public void setEventId(Event eventId) {
        this.eventId = eventId;
    }

    public void addUserToUserCollection(User user) {
        if(userCollection == null){
            this.userCollection = new HashSet<>();
        }
        this.userCollection.add(user);
    }

    public void removeUserFromUserCollection(User user) {
        if(userCollection == null){
            this.userCollection = new HashSet<>();
        }
        this.userCollection.remove(user);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Notification)) {
            return false;
        }
        Notification other = (Notification) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.aljfce.business.entity.Notification[ id=" + id + " ]";
    }

}
